{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ",\"中标日期\",\"采购人联系方式\",\"采购人\",\"评标委员会成员名单\",\"采购项目名称\",\"总成交金额\",\"采购单位\",\"行政区域\",\"采购单位联系方式\",\"谈判小组、询价小组成员名单及单一来源采购人员名单\",\"成交日期\",\"品目\",\"项目联系人\",\"公告时间\",\"代理机构地址\",\"评审专家名单\",\"总中标金额\",\"采购单位地址\",\"采购人地址\",\"本项目招标公告日期\",\"项目联系电话\",\"代理机构名称\",\"定标日期\",\"代理机构联系方式\",\"==========\"\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf8 -*-\n",
    "import glob\n",
    "#files = glob.glob('../text_data_ccgp_fuwuqi_gkzb/*_table')\n",
    "files = glob.glob('../text_data_ccgp_fuwuqi_zbgg/*_table')\n",
    "item_set = set()\n",
    "for file in files:\n",
    "    infile = open(file)\n",
    "    for n, line in enumerate(infile):\n",
    "        if n < 2:\n",
    "            continue\n",
    "        line = line.strip().split('\\t', 1)[0].strip()\n",
    "        if \"货物\" in line or \"服务\" in line or \"附件\" in line:\n",
    "            continue\n",
    "        item_set.add(line)\n",
    "        #if line == '货物/通用设备/计算机设备及软件/计算机网络设备/终端接入设备,':\n",
    "            #print file\n",
    "strtmp = \"\"\n",
    "for item in item_set:\n",
    "    strtmp = strtmp + ',\"' + item + '\"'\n",
    "    \n",
    "print strtmp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../text_data_ccgp_fuwuqi_zbgg/3287_table\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf8 -*-\n",
    "import glob\n",
    "#iles = glob.glob('../text_data_ccgp_fuwuqi_gkzb/*_table')\n",
    "files = glob.glob('../text_data_ccgp_fuwuqi_zbgg/*_table')\n",
    "#item_list = [\"采购人联系方式\",\"采购人\",\"采购项目名称\",\"采购单位\",\"获取招标文件时间\",\"行政区域\",\"采购单位联系方式\",\"品目\",\"获取招标文件的地点\",\"项目联系人\",\"公告时间\",\"代理机构地址\",\"预算金额\",\"采购单位地址\",\"采购人地址\",\"开标时间\",\"项目联系电话\",\"代理机构名称\",\"代理机构联系方式\",\"开标地点\",\"招标文件售价\"]\n",
    "item_list = [\"中标日期\",\"采购人联系方式\",\"采购人\",\"评标委员会成员名单\",\"采购项目名称\",\"总成交金额\",\"采购单位\",\"行政区域\",\"采购单位联系方式\",\"谈判小组、询价小组成员名单及单一来源采购人员名单\",\"成交日期\",\"品目\",\"项目联系人\",\"公告时间\",\"代理机构地址\",\"评审专家名单\",\"总中标金额\",\"采购单位地址\",\"采购人地址\",\"本项目招标公告日期\",\"项目联系电话\",\"代理机构名称\",\"定标日期\",\"代理机构联系方式\"]\n",
    "#outfile = open('ccgp_fuwuqi_gkzb.csv', 'w')\n",
    "outfile = open('ccgp_fuwuqi_zbgg.csv', 'w')\n",
    "\n",
    "strtmp = '招标题目' + '\\t' + '地域'\n",
    "for item in item_list:\n",
    "    strtmp = strtmp + '\\t' + item\n",
    "print >> outfile, strtmp\n",
    "\n",
    "for _, file in enumerate(files):\n",
    "    #if _ > 10:\n",
    "    #    break\n",
    "    title_list = []\n",
    "    content_list = []\n",
    "    infile = open(file)\n",
    "    titleee = ''\n",
    "    location = ''\n",
    "    \n",
    "    for n, line in enumerate(infile):\n",
    "        if n == 0:\n",
    "            titleee = line.strip()\n",
    "            if titleee == '水利部太湖流域管理局太湖流域洪水风险图管理与应用系统服务器及系统软件购置中标公告':\n",
    "                print file\n",
    "            continue\n",
    "        if n == 1:\n",
    "            location = line.strip()\n",
    "            continue\n",
    "        if n == 2: #======\n",
    "            continue\n",
    "        line = line.strip().split('\\t', 1)\n",
    "        if \"货物\" in line or \"服务\" in line or \"附件\" in line:\n",
    "            continue\n",
    "        if len(line) < 2:\n",
    "            continue\n",
    "        title_list.append(line[0].strip())\n",
    "        if line[1].strip() != '公告时间' and line[1].strip() != '中标日期':\n",
    "            content_list.append(line[1].strip())\n",
    "    content_len = len(content_list)\n",
    "    com_dic = {}\n",
    "    for n, title in enumerate(title_list):\n",
    "        if n < content_len:\n",
    "            if title in item_list:\n",
    "                if content_list[n] != '详见公告正文':\n",
    "                    com_dic[title] = content_list[n]\n",
    "    if len(com_dic) < 3:\n",
    "        continue\n",
    "    #Debugging\n",
    "    #for item in com_dic:\n",
    "    #    print item + ':' + com_dic[item]\n",
    "    \n",
    "    strtmp = titleee + '\\t' + location\n",
    "    for item in item_list:\n",
    "        if item in com_dic:\n",
    "            strtmp = strtmp + '\\t' + com_dic[item].strip()\n",
    "        else:\n",
    "            strtmp = strtmp + '\\t' + 'NA'\n",
    "    print >> outfile, strtmp.strip()\n",
    "    \n",
    "        \n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
